package com.atguigu.yygh.hosp.service.impl;

import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.hosp.mapper.HospitalSetMapper;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.order.SignInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * 医院设置接口
 * @author ybl
 * @version 1.9
 * @date 2021/5/12 22:47
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper, HospitalSet> implements HospitalSetService {

  // 根据hoscode 返回sign
  @Override
  public String getSignKey(String hoscode) {
    HospitalSet hospitalSet = this.getByHoscode(hoscode);
    if(null == hospitalSet) {
      throw new YyghException(ResultCodeEnum.HOSPITAL_OPEN);
    }
    if(hospitalSet.getStatus().intValue() == 0) {
      throw new YyghException(ResultCodeEnum.HOSPITAL_LOCK);
    }
    return hospitalSet.getSignKey();
  }

  // 获取医院签名信息
  @Override
  public SignInfoVo getSignInfoVo(String hoscode) {
    SignInfoVo signInfoVo = new SignInfoVo();
    HospitalSet hospitalSet = baseMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
    if (hospitalSet == null) {
      throw new YyghException(ResultCodeEnum.HOSPITAL_OPEN);
    }
    signInfoVo.setApiUrl(hospitalSet.getApiUrl());
    signInfoVo.setSignKey(hospitalSet.getSignKey());
    return signInfoVo;
  }

  /**
   * 根据hoscode获取医院设置
   * @param hoscode
   * @return
   */
  private HospitalSet getByHoscode(String hoscode) {
    return baseMapper.selectOne(new QueryWrapper<HospitalSet>().eq("hoscode", hoscode));
  }


}
